<?
/*
-------------------------------
Script - EvaGb
-------------------------------
File - mod_case.class.php
-------------------------------
Function - Класс модель
-------------------------------
*/
class casy {
  private $db,$tpl;
  public  $result,$error;
  function __construct() {
      $this->db=locator::get()->db();
      $this->tpl=locator::get()->tpl();
  }
  function get_content() {
         $page=locator::get()->page;
         $page=($page-1)*$GLOBALS['_gb_cnf']['page_num'];
         if ($page<0) $page=0;
         $sql=$this->db->query("
                      SELECT P.*,B.ip as `is_banned` FROM {$this->db->pref}_post as P
                         LEFT JOIN {$this->db->pref}_badip as B ON(B.fild=P.id_post)
                      ORDER BY {$GLOBALS['_gb_cnf']['order_by']}
                      LIMIT $page,{$GLOBALS['_gb_cnf']['page_num']}") or die(mysql_error());  // or die() наверно можно и убрать?
         if (mysql_num_rows($sql)) {
             include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/onlinereplace_inc.php";
             $this->tpl->set('result',$this->formatemsg($sql));
             return $this->tpl->load("text.tpl");
         }else {
             $this->error.=display::info("Ошибка: По данному критерию ничего не найдено");
         }
  }
  function get_page() {
     include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/pager_inc.php";
     $page=locator::get()->page;
     $sql=$this->db->query("SELECT COUNT(*) FROM {$this->db->pref}_post");
     if (mysql_num_rows($sql)) {
          return pager(
             array('page_num'=>$GLOBALS['_gb_cnf']['page_num'],'page_block'=>$GLOBALS['_gb_cnf']['page_block']),
             $page?$page:1,
             str_replace("{link}","?page={i}",$GLOBALS['_gb_cnf']['tpl_link']),
             $GLOBALS['_gb_cnf']['tpl_blink'],
             $this->db->result($sql)
          );
     }
  }
  private function formatemsg($sql,$hl=0) {
      include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/onlinereplace_inc.php";
      $arr=array();
        while($row=$this->db->fetch_array($sql)) {
                 $row['text']=onlinereplace($row['text']);
                 if ($hl) {
                     $row['name']=str_replace($hl,"<span class='search'>$hl</span>",$row['name']);
                     $row['text']=str_replace($hl,"<span class='search'>$hl</span>",$row['text']);
                 }
                 $row['addfilds']=unserialize($row['addfilds']);
                 $row['data']=date($GLOBALS['_gb_cnf']['data_format'],$row['data']);
                 $arr[]=$row;
        }
      return $arr;
  }
  function debug() {
     debug();
  }
  function search() {
         include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/pager_inc.php";
         $page=locator::get()->page;
         $page=($page-1)*$GLOBALS['_gb_cnf']['page_num'];
         if ($page<0) $page=0;
         $search=mysql_real_escape_string($_GET['search']);
         $sql=$this->db->query("
                      SELECT P.*,B.ip as `is_banned` FROM {$this->db->pref}_post as P
                         LEFT JOIN {$this->db->pref}_badip as B ON(B.fild=P.id_post)
                      WHERE P.text LIKE('%$search%') OR P.name LIKE('%$search%')
                      ORDER BY {$GLOBALS['_gb_cnf']['order_by']}
                      LIMIT $page,{$GLOBALS['_gb_cnf']['page_num']}") or die(mysql_error());  // or die() наверно можно и убрать?
         if (mysql_num_rows($sql)) {
             $this->tpl->set('result',$this->formatemsg($sql,$search));
             $this->tpl->set("is_search",true);
             $this->result['content']=$this->tpl->load("text.tpl");
             $this->result['form']='';
             $sql=$this->db->query("SELECT COUNT(*) FROM {$this->db->pref}_post WHERE text LIKE('%$search%') OR name LIKE('%$search%')");
             $page=locator::get()->page;
             $this->result['page']=pager(
                      array('page_num'=>$GLOBALS['_gb_cnf']['page_num'],'page_block'=>$GLOBALS['_gb_cnf']['page_block']),
                      $page?$page:1,
                      str_replace("{link}","?search={$_GET['search']}&page={i}",$GLOBALS['_gb_cnf']['tpl_link']),
                      $GLOBALS['_gb_cnf']['tpl_blink'],
                      $this->db->result($sql)
                      );
            return true;
         }else {
             $this->result['error']=display::info("Ошибка: Ничего не найдено");
         }
     return false;
  }
  function get_form() {
      if ($GLOBALS['_gb_cnf']['addfild']) {
          $this->tpl->set('addfild',$GLOBALS['_gb_cnf']['addfild']);
      }
      $this->tpl->set("img","<img src='{$GLOBALS['_gb_cnf']['dir']}/sis/inc/img/img.php'>");
      $this->tpl->set("namesave",htmlspecialchars($_COOKIE['_gb_user_namesave'],ENT_QUOTES));
      $_COOKIE['_gb_user_addfildsave']=unserialize($_COOKIE['_gb_user_addfildsave']);
      if (!empty($_COOKIE['_gb_user_addfildsave'])) {
          foreach($_COOKIE['_gb_user_addfildsave'] as $key=>$val) {
              $_COOKIE['_gb_user_addfildsave'][$key]=htmlspecialchars($val,ENT_QUOTES);
          }
      }
      $this->tpl->set("addfildsave",$_COOKIE['_gb_user_addfildsave']);
      return $this->tpl->load('form.tpl');
  }
  function get_past_id() {
      return @$this->db->result($this->db->query("SELECT id_post FROM {$this->db->pref}_post ORDER BY id_post DESC LIMIT 1"));
  }
  function add_msg() {
    if($_SESSION['_gb_nospam']) {
      include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/replace_inc.php";
      extract(givepost("name,text"));
      $text=mysql_real_escape_string(replace($text));
      $name=mysql_real_escape_string(mreplace($name));
      if ($GLOBALS['_gb_cnf']['addfild']){
           $addfild_s=serialform($GLOBALS['_gb_cnf']['addfild'],0);
           $addfild=serialform($GLOBALS['_gb_cnf']['addfild']);
      }
      if ($this->db->insert("post","NULL,'$name','$text','".time()."','$addfild','".$_SERVER['REMOTE_ADDR']."'")) {
          $_SESSION['_gb_nospam']=array();
          $_SESSION['_gb_nospam']['time']=time();
          $_SESSION['_gb_nospam']['id']=mysql_insert_id();
          $this->error.=display::info("Сообщение успешно добавлено");
          setcookie("_gb_user_namesave",html_entity_decode($name,ENT_QUOTES),time()+999999999,"/");
          setcookie("_gb_user_addfildsave",$addfild_s,time()+999999999,"/");
          header("Refresh:2");
      }else {
          $this->error.=display::info("Ошибка: Не могу добавить сообщение");
      }
    } else {
       $this->error.=display::info("Ошибка: Антиспам заблокировал попытку добавить сообщение");
    }
  }
  function add_update_msg() {
      include_once $GLOBALS['_gb_cnf']['dir']."/sis/inc/replace_inc.php";
      extract(givepost("text"));
      $GLOBALS['_gb_cnf']['no_spam_msg']=str_replace("{time}",date($GLOBALS['_gb_cnf']['data_format'],time()),$GLOBALS['_gb_cnf']['no_spam_msg']);
      $text=replace($GLOBALS['_gb_cnf']['no_spam_msg'].$text);
      $text=mysql_real_escape_string($this->db->result($this->db->query("SELECT `text` FROM {$this->db->pref}_post WHERE id_post='{$_SESSION['_gb_nospam']['id']}'")).$text);
      if ($this->db->update("post","text='$text' WHERE id_post='{$_SESSION['_gb_nospam']['id']}'")) {
          $this->error.=display::info("Сообщение успешно добавлено");
          header("Refresh:2");
      }else {
         $this->error.=display::info("Ошибка: Не могу добавить сообщение");
      }
  }
  function is_banned() {
      return mysql_num_rows($this->db->query("SELECT * FROM {$this->db->pref}_badip WHERE ip='".md5($_SERVER['REMOTE_ADDR'])."' AND ".time()."-btime<=etime"));
  }
}
?>
